# encoding=utf8
# author: allen@thecover.co
# create_time: 2024/9/10 14:55
# file: __init__.py
import os
from os.path import join, dirname, realpath, exists
import yaml
import ujson as json

SOURCE_PATH = dirname(realpath(__file__))
PROJECT_PATH = dirname(SOURCE_PATH)
DATA_PATH = join(PROJECT_PATH, 'data')
LOG_PATH = join(PROJECT_PATH, 'log')


# 读取配置文件
CONF_PATH = join(PROJECT_PATH, 'config')
if exists(CONF_PATH):
    try:
        with open(join(CONF_PATH, 'conf.yml'), 'r', encoding='utf-8') as f:
            yaml_cfg = yaml.load(f, Loader=yaml.FullLoader)
        with open(join(CONF_PATH, 'conf-test.yml'), 'r', encoding='utf-8') as f:
            yaml_cfg_test = yaml.load(f, Loader=yaml.FullLoader)
        with open(join(CONF_PATH, 'conf-prod.yml'), 'r', encoding='utf-8') as f:
            yaml_cfg_pro = yaml.load(f, Loader=yaml.FullLoader)

        if os.environ.get("ACTIVATE"):
            yaml_cfg.update({"activate": os.environ.get("ACTIVATE")})

        if yaml_cfg['activate'] == 'product':
            yaml_cfg.update(yaml_cfg_pro)
        elif yaml_cfg['activate'] == 'test':
            yaml_cfg.update(yaml_cfg_test)
        else:
            raise Exception(f"Get wrong activate type: {yaml_cfg['activate']}.")

        print(yaml_cfg)
        print(yaml_cfg['mongo']['url'].format(db='test'))
    except Exception:
        raise
else:
    raise Exception("Can't read the configs. Check the file path.")
